{% extends "base.html" %}
{% block content %}
<h1>Cutting Form View</h1>
<p>
  As named, Form Views are Views... and thus benefit of all their
  functionnalities. Here are demoed inheritage and stopping process on
  misbehaving</p>
<p>
  To stop process, enter for instance <code>\'</code> in username or password...</p>
<form action="" method="POST">
{{ form }}
<input type="submit">
</form>
<div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">obviews.views</span> <span class="kn">import</span> <span class="n">View</span><span class="p">,</span> <span class="n">FormView</span><span class="p">,</span> <span class="n">CutResponse</span>

<span class="c"># cutting throug</span>
<span class="k">class</span> <span class="nc">CutForm</span><span class="p">(</span><span class="n">SimplestForm</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;forms/cutting_form.html&quot;</span>

    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">variables</span><span class="p">):</span>
        <span class="k">if</span> <span class="s">r&#39;</span><span class="se">\&#39;</span><span class="s">&#39;</span> <span class="ow">in</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;username&quot;</span><span class="p">]</span> \
               <span class="ow">or</span> <span class="s">r&#39;</span><span class="se">\&#39;</span><span class="s">&#39;</span> <span class="ow">in</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;password&quot;</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="n">CutResponse</span><span class="p">(</span><span class="n">HttpResponseForbidden</span><span class="p">(</span>
                <span class="s">&quot;Playing around with BAD injection, got a nice 403&quot;</span><span class="p">))</span>

<span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>                
    <span class="p">(</span><span class="s">&#39;^form/cut/$&#39;</span><span class="p">,</span> <span class="n">CutForm</span><span class="p">()),</span>
<span class="p">)</span>

</pre></div>
<p>
  Ok, you are asking what about Ajax? In-place forms? web2.0... and
  all... I am not the man. Sorry, but you do have a slightly adapted
  class <strong>next</strong> that can help, the
  <code>AjaxFormView</code> which does not make Ajax calls by itself
  but which encapsulate a mechanism to <a href="/ajax/">safely
  fallback on regular form if JS not activated</a>.
</p>
{% endblock %}
